<?php
namespace app\model\question;

use think\Model;
use app\model\question\ZxlKnowStoreQuestionUserStatisticsModel;

class ZxlKnowStoreQuestionChapterModel extends Model
{
    protected $name = 'zxl_know_store_question_chapter';

    /**
     * 获取章节知识点：获取当前分类所有下级分类
     * 
     * @return array
     */
    public function children(){
        return $this->hasMany(ZxlKnowStoreQuestionChapterModel::class, "pid", "id")->field("id, name, pid, sort")->order("sort", "asc");
    }

    // 获取问题章节信息
    public static function getChapterName($cpid){
        $name = '';
        $info = self::where("id", $cpid)->findOrEmpty();
        if(!$info->isEmpty()) {
            $name = $info['name'];
        }
        return $name;
    }
    /**
     * 获取节的题目数量
     * 
     * @return number
     */
    public function getQnumberAttr(){
        
        if($this->pid > 0){          
            return ZxlKnowStoreQuestionModel::where("chapter_id", $this->id)->where("is_delete", 0)->count();
        } else {
            $ids = self::where("pid", $this->id)->column("id");
            return ZxlKnowStoreQuestionModel::whereIn("chapter_id", $ids)->whereOr("chapter_id", $this->id)->where("is_delete", 0)->count();   
        }
    }

    /**
     * 获取所有章节信息，父节点为0附带所有子节点 用于tree数据格式
     * 
     * @return array
     */
    public static function getAllchapter($product_id, $user_id, $page, $limit){
        $list = self::with(['children' => function($q){
            $q->where('is_practice', 1)
            ->field('id, m_id, name, sort, is_trial, pid')
            ->append(['qnumber'])
            ->order('sort', 'asc');
        }])->where(['pid' => 0, 'lib_id' => $product_id, 'is_practice' => 1])
        ->field('id, m_id, name, sort, is_trial, pid')
        ->append(['qnumber'])
        ->order('sort', 'asc')
        ->paginate($limit,false,['page'=>$page])
        ->toArray();
        if($list['data']){
            foreach($list['data'] as &$item){
                $item['right_rate'] = 0;
                $item['practice_number'] = 0;
                $item['practice_rate'] = 0;
                $practiceInfo = ZxlKnowStoreQuestionUserStatisticsModel::where(['user_id' => $user_id, 'target_id' => $item['id']])->find();
                if($practiceInfo){
                    $item['right_rate'] = $practiceInfo['right_rate'];
                    $item['practice_number'] = $practiceInfo['practice_number'];
                    if($item['qnumber'] > 0 && $item['practice_number'] > 0){
                        $item['practice_rate'] = bcdiv($item['practice_number'], $item['qnumber'], 2)*100;
                    }
                }
                if(!empty($item['children'])) {
                    foreach($item['children'] as &$child) {
                        $child['right_rate'] = 0;
                        $child['practice_number'] = 0;
                        $child['practice_rate'] = 0;
                        $chilPracticeInfo = ZxlKnowStoreQuestionUserStatisticsModel::where(['user_id' => $user_id, 'target_id' => $child['id']])->find();
                        if($chilPracticeInfo){
                            $child['right_rate'] = $chilPracticeInfo['right_rate'];
                            $child['practice_number'] = $chilPracticeInfo['practice_number'];
                            if($child['qnumber'] > 0 && $child['practice_number'] > 0){
                                $child['practice_rate'] = bcdiv($child['practice_number'], $child['qnumber'], 2)*100;
                            }
                        }
                    }
                }
            }
        }
        return $list;
    }
}